package calvin.study.redis.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import calvin.study.redis.config.RedisConstants;
import calvin.study.redis.entity.User;
import calvin.study.redis.utils.RedisUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;

@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private RedisUtil redisUtil;

	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	@PostMapping("reg")
	public String regUser(User user) {
		String userId = redisUtil.getSeqId();
		try {
			user.setUserId(userId);
			Map<String, Object> userMap = Convert.toMap(String.class, Object.class, user);
			redisUtil.hmSet(RedisConstants.REDIS_KEY_USER_REG + userId, userMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "用户注册失败";
		}
		return "用户[" + userId + "]注册成功";
	}
	
	/**
	 * @Title: getUser   
	 * @Description: 查询该用户信息
	 * @date: 2022年1月26日 上午11:30:09   
	 * @version:
	 * @param userId
	 * @return          
	 * @throws
	 */
	@GetMapping("getUser")
	public String getUser(String userId) {
		User user = redisUtil.hGetToBean(RedisConstants.REDIS_KEY_USER_REG + userId,User.class);
		return JSONUtil.toJsonStr(user);
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	@GetMapping("getAll")
	public String getAll() {
		List<User> userList = redisUtil.hGetList("hash:user:*",User.class);
		return JSONUtil.toJsonStr(userList);
	}
	
	@PostMapping("focus")
	public String focus(String userId, String focusId) {
		if(userId.equals(focusId)) {
			return "不可以关注自己";
		}
		//生成该用户关注的记录
		String guanzhuKey = RedisConstants.REDIS_KEY_USER_GUANZHU + userId;
		redisUtil.sAdd(guanzhuKey,focusId);
		System.out.println("用户[" + userId + "]，关注了[" + focusId + "]");
		
		//生成被关注用户的粉丝记录
		String fansKey = RedisConstants.REDIS_KEY_USER_FANS + focusId;
		redisUtil.sAdd(fansKey, userId);
		System.out.println("用户[" + focusId + "]，是[" + userId + "]的粉丝");
		return "用户[" + userId + "]，关注[" + focusId + "]";
	}
	
	/**
	 * 我关注的用户
	 * @param userId
	 * @return
	 */
	@GetMapping("myFocus")
	public String myFocus(String userId) {
		//查询我关注的人
		Set<Object> users = redisUtil.sGet(RedisConstants.REDIS_KEY_USER_GUANZHU + userId);
		Iterator<Object> it = users.iterator();
		List<User> userList = new ArrayList<User>();
		while(it.hasNext()) {
			String focusUserId  = it.next().toString();
			//查询粉丝是否关注了我 
			User user = redisUtil.hGetToBean(RedisConstants.REDIS_KEY_USER_REG + focusUserId,User.class);
			//是否相互关注
			boolean isFocus = redisUtil.sIsMember(RedisConstants.REDIS_KEY_USER_GUANZHU + focusUserId, userId);
			if(isFocus) {
				user.setTogetherFocus(1);
			}
			userList.add(user);
		}
		return JSONUtil.toJsonStr(userList);
	}
	
	
	
	/**
	 * 我的粉丝
	 * @param userId
	 * @return
	 */
	@GetMapping("myFans")
	public String myFans(String userId) {
		List<User> fansList = null;
		//1.查询关注自己的人
		Set<Object> users = redisUtil.sGet(RedisConstants.REDIS_KEY_USER_FANS + userId);
		if(CollectionUtils.isEmpty(users)) {
			return JSONUtil.toJsonStr(fansList);
		}
		
		fansList = new ArrayList<User>();
		Iterator<Object> it = users.iterator();
		while(it.hasNext()) {
			String fansId = it.next().toString();
			User fans = redisUtil.hGetToBean(RedisConstants.REDIS_KEY_USER_REG + fansId,User.class);
			//查询粉丝是否关注了自己
			boolean isFocus = redisUtil.sIsMember(RedisConstants.REDIS_KEY_USER_GUANZHU + fansId, userId);
			if(isFocus) {
				fans.setTogetherFocus(1);
			}
			fansList.add(fans);
		}
		return JSONUtil.toJsonStr(fansList);
	}
	

}
